package vip.xiaonuo.biz.modular.pay.util.bankpay;


import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import org.springframework.http.HttpHeaders;
import vip.xiaonuo.common.exception.CommonException;

/**
 * ClassName:XmlRequest
 * Description:
 *
 * @Author:薛守立
 * @CreateTime:2024/9/19 11:25
 */
public class XmlRequest<T> {

   public ResponseEntity<String> postRequest(T object, String url) {
       // javabean转化为xml
       String xmlPlayLoad = XmlUtils.javaBeanToXml(object);
       RestTemplate restTemplate = new RestTemplate();

       // 设置请求
       HttpHeaders headers = new HttpHeaders();
       headers.setContentType(MediaType.APPLICATION_XML);
       HttpEntity<String> entity = new HttpEntity<>(xmlPlayLoad, headers);

       ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
       // 处理响应
       if(response.getStatusCode().is2xxSuccessful()){
           return response;
       }else{
           // 错误处理
           throw new CommonException("支付接口响应网络错误，请检查网路服务"+response.getStatusCode());
       }
   }


}
